﻿using SachOnlineLibrary.BLL;
using SachOnlineLibrary.Public;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UC_Admin_UCAdminLienHe : System.Web.UI.UserControl
{
    LienHeBLL lienhe = new LienHeBLL();
    public static List<LienHe> listlienhe = new List<LienHe>();
    public string CheckTrangThai(object obj)
    {
        bool b = bool.Parse(obj.ToString());

        if (b)
            return "Đã xem";
        else
            return "Chưa xem";
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MultiView1.ActiveViewIndex = 0;         
            GridviewLienHe_Load();
        }       
    }
    private void GridviewLienHe_Load(bool trangthai)
    {
        listlienhe = lienhe.LietKeTatCaLienHe();
        GridView1.DataSource = listlienhe.Where(s => s.Trangthai.Equals(trangthai)).ToList();
        GridView1.DataBind();
    }
    private void GridviewLienHe_Load()
    {
        listlienhe = lienhe.LietKeTatCaLienHe();
        GridView1.DataSource = listlienhe;
        GridView1.DataBind();
    }

    protected void ddlLoaiLienHe_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (int.Parse(ddlLoaiLienHe.SelectedValue.ToString().Trim()) == 1)
        {
            GridviewLienHe_Load();
        }
        else
        {
            if (int.Parse(ddlLoaiLienHe.SelectedIndex.ToString().Trim()) == 2)
            {
                GridviewLienHe_Load(false);
            }
            else
            {
                GridviewLienHe_Load(true);
            }
        }
       
    }

    protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        string name = e.CommandName;
        int id = Convert.ToInt32(e.CommandArgument.ToString());
        LienHe lh = new LienHe();       
        switch (name)
        {
            case "xoa":
                //       lienhe_info.Delete(id);
                bool tam = lienhe.Xoalh(id);
                if(tam == true)
                    Response.Redirect("~/Page_Admin/DefaultAdmin.aspx?page=lienhe");                
                break;
            case "xem":
                lh = lienhe.LietKeTheoLienHeTheoMa(id);
                txthoten.Text = lh.Ten;
                txtdienthoai.Text = lh.SDT;
                txtemail.Text = lh.Email;
                txtghichu.Text = lh.Noidung;
                HiddenField1.Value = id.ToString();
                ckbDaDoc.Checked = lh.Trangthai;
                MultiView1.ActiveViewIndex = 1;
                break;
        }
    }
    protected void bntQuayLai_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Page_Admin/DefaultAdmin.aspx?page=lienhe"); 
    }
    protected void bntCapNhat_Click(object sender, EventArgs e)
    {
        int id = int.Parse(HiddenField1.Value);
        bool trangthai = ckbDaDoc.Checked;
        if (trangthai == true)
            trangthai = false;
        else
            trangthai = true;
        if (lienhe.ThayDoiTrangThai(id, trangthai))
        {
            lblThongBao.Text = "Cập nhật trạng thái thành công";
            btnCapNhat.Enabled = false;
        }
        else
        {
            lblThongBao.Text = "Cập nhật trạng thái không thành công";
        }
    }

    protected void chkTest_CheckedChanged(object sender, EventArgs e)
    {
        btnCapNhat.Enabled = true;
    }
}